/**
 * Created by gaoyj on 2018/6/27.
 */
window.onload = function (ev) {
    topbar.hide();
};

var modelTitle;
$.post("/UserAnnouncementController/getAnnouncementModel", null, function (data) {
    var group_obj = data;
    for (var i = 0; i < group_obj.length; i++) {
        modelTitle += '<option value="' + group_obj[i].id + '">' + group_obj[i].title + '</option>'
    }
    $("#modelTitle").append(modelTitle);
});

function reloadContent(obj) {
    $("#codeParam").html("");
    $.post("/UserAnnouncementController/getAnnouncementModelContent", {id: $("#modelTitle").val()}, function (data) {
        editor2.html(data);
        var regex = new RegExp("#code", "g")
        var result = data.match(regex);
        var count = !result ? 0 : result.length;
        var str = '';
        for (var i = 0; i < count; i++) {
            var codeCount = "#code" + (i + 1) + "#";
            str += '<input type="text" class="form-control code" placeholder="' + codeCount + '"   style="width: 150px;"/>';
        }
        $("#codeParam").append(str);
    });
}

$("#addCode").click(function () {
    var data = editor2.html();
    $(".code").each(function () {
        var param = $(this).val();
        var placeholder = $(this).attr('placeholder');
        if (param != "") {
            data = data.replace(placeholder, param);
            $(this).attr("readOnly", 'true');
        }
    })
    editor2.html(data);
})
$("#clearCode").click(function () {
    reloadContent()
})


$("#addModelSubmit").click(function () {
    if (editor2.html().indexOf("#code") != -1) {
        layer.msg("请传入参数并传参!");
        return;
    }
    editor.html(editor2.html())
    $('#templateModal').modal('hide')
})
$("#submitMessage").click(function () {
    var url;
    if ($("#hidOperate").val() == '0') {
        url = "UserAnnouncementController/updateAnnouncement"
    } else {
        url = "UserAnnouncementController/addAnnouncement"
    }
    var modelId=$("#supplyModel").val();
    if(modelId!=null&&modelId!=""){
        modelId=$("#supplyModel").val().join(",");
    }
    $.ajax({
        type: "POST",
        url: url,
        data: {
            'id': $("#hidId").val(),
            'title': $("#title").val(),
            'author': $("#author").val(),
            'modelId': modelId,
            'content': editor.html(),
            'state': $("input[name='state']:checked").val(),
            'readable': $("#readable").val(),
            'operate': $("#hidOperate").val()
        },
        dataType: "json",
        success: function (data) {
            if (data.success) {
                layer.msg(data.message)
                setTimeout(closeActive(), 1500);
                addTab('userAnnouncement', '1', '会员公告');
            } else {
                layer.msg(data.message);
            }
        },
        beforeSend: function () {
        }
    });
})

var editor;
var editor2;
var options = {
    cssPath: 'js/kindeditor-4.1.10/plugins/code/prettify.css',
    filterMode: true,
    resizeType: 1,
    allowPreviewEmoticons: false,
    allowImageUpload: true,
    allowFileManager: true,
    uploadJson: '/news/getPicUrl',
    readonlyMode: true,
    fileManagerJson: '/js/kindeditor-4.1.10/jsp/file_manager_json.jsp',
    afterUpload: function () {
        this.sync();
    },
    afterBlur: function () {
        this.sync();
    },
    width: '100%',
    height: '232px',
    items: [
        'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline']
};
KindEditor
    .ready(function (K) {
        editor = K.create('#content', options);
        editor2 = K.create('#modelContent', options);
    });